package com.wyhello.tube4.spring.model;

import java.math.BigInteger;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.web.servlet.ModelAndView;

import com.wyhello.tube4.facade.TubeFacade;
import com.wyhello.tube4.pojo.Position;
import com.wyhello.tube4.pojo.Reality;
import com.wyhello.tube4.pojo.Tube;

public class AnalysisDrawModel {
	private TubeFacade facade;

	public TubeFacade getFacade() {
		return facade;
	}

	public void setFacade(TubeFacade facade) {
		this.facade = facade;
	}
	public List<Reality> queryRealityTime(int positionId,String begin,String end, String type){
		List<Reality> realitys = facade.queryRealityTime(positionId, begin, end, type);
		return realitys;
	}

	/**
	 * 根据时间范围查询某一个管的测量记录
	 * @param tubeId
	 * @param begin
	 * @param end
	 * @return
	 */
	public Set<Position> getMeasuringRecordByTime(int tubeId,String begin,String end, String type)
	{
		Tube tube = facade.getTubeById(tubeId);
		Set<Position> positions = tube.getPositions();

		for (Position position : positions) {
			List<Reality> realitys = facade.queryRealityTime(position.getId(), begin, end, type);

			Set<Reality> setRealitys = new HashSet<Reality>();
			for(Reality reality: realitys){
				setRealitys.add(reality);
			}
			position.setRealitys(setRealitys);

		}

		return positions;
	}

	/**
	 * 根据时间范围查询某一个位置的测量记录
	 * @param positionId
	 * @param begin
	 * @param end
	 * @return
	 */
	public List<Reality> getMeasuringRecordByTimeAndPositionId(int positionId,String begin,String end, String type)
	{
		List<Reality> realitys = facade.queryRealityTime(positionId, begin, end, type);
		return realitys;
	}

	/**
	 * 查询field在positionIds里所有测量记录中的最大值
	 * @param field 字段
	 * @param positionIds 位置id
	 * @return
	 */
	public String queryRealityMaxByPosIds(String field,String positionIds,String bagId){
		return facade.queryRealityMaxByPosIds(field, positionIds, bagId);
	}
	/**
	 * 查询field在positionIds里所有测量记录中的最小值
	 * @param field
	 * @param positionIds
	 * @return
	 */
	public String queryRealityMinByPosIds(String field,String positionIds,String bagId){
		return facade.queryRealityMinByPosIds(field, positionIds,bagId);
	}
	/**
	 * 查询field在positionIds里所有测量记录中的平均值
	 * @param field
	 * @param positionIds
	 * @return
	 */
	public String queryRealityAvgByPosIds(String field,String positionIds,String bagId){
		return facade.queryRealityAvgByPosIds(field, positionIds,bagId);
	}
	
	

	public ModelAndView getAnyliseRow(String field, String begin, String end, int rowId) {

		Map<Date,Float>  max = new HashMap<Date, Float>();
		Map<Date,Float>  min = new HashMap<Date, Float>();
		Map<Date,Double>  avg = new HashMap<Date, Double>();

		List list = facade.getAnyliseRow(field, begin, end, rowId);
		Iterator it = list.iterator();
		while(it.hasNext()){

			Object[] row = (Object[])it.next();
			Date _time = (Date)row[0];
			Float _max = (Float)row[1];
			Float _min = (Float)row[2];
			Double _avg = (Double)row[3];
			max.put(_time, _max);
			min.put(_time, _min);
			avg.put(_time, _avg);
		}

		ModelAndView model = new ModelAndView(field);
		model.addObject("max", max);
		model.addObject("min", min);
		model.addObject("avg", avg);
		return model;

	}

	public ModelAndView getAnyliseSurface(String field, String begin, String end, int surfaceId) {

		Map<Date,Float>  max = new HashMap<Date, Float>();
		Map<Date,Float>  min = new HashMap<Date, Float>();
		Map<Date,Double>  avg = new HashMap<Date, Double>();

		List list = facade.getAnyliseSurface(field, begin, end, surfaceId);
		Iterator it = list.iterator();
		//int i = 0;
		while(it.hasNext()){
			//i++;
			Object[] row = (Object[])it.next();
			Date _time = (Date)row[0];
			Float _max = (Float)row[1];
			Float _min = (Float)row[2];
			Double _avg = (Double)row[3];
			max.put(_time, _max);
			min.put(_time, _min);
			avg.put(_time, _avg);
			//System.out.println(i);
		}

		ModelAndView model = new ModelAndView(field);
		model.addObject("max", max);
		model.addObject("min", min);
		model.addObject("avg", avg);
		return model;

	}

	public ModelAndView getAnyliseBoiler(String field, String begin, String end, int boilerId) {

		Map<Date,Float>  max = new HashMap<Date, Float>();
		Map<Date,Float>  min = new HashMap<Date, Float>();
		Map<Date,Double>  avg = new HashMap<Date, Double>();

		List list = facade.getAnyliseBoiler(field, begin, end, boilerId);
		Iterator it = list.iterator();
		//int i = 0;
		while(it.hasNext()){
			//i++;
			Object[] row = (Object[])it.next();
			Date _time = (Date)row[0];
			Float _max = (Float)row[1];
			Float _min = (Float)row[2];
			Double _avg = (Double)row[3];
			max.put(_time, _max);
			min.put(_time, _min);
			avg.put(_time, _avg);
			//System.out.println(i);
		}

		ModelAndView model = new ModelAndView(field);
		model.addObject("max", max);
		model.addObject("min", min);
		model.addObject("avg", avg);
		return model;

	}


	public ModelAndView getRealityPieRow(String realField, String warnField, int rowId){
		
		List list = facade.getRealityPieRow(realField, warnField, rowId);
		Iterator it = list.iterator();
		int warn = 0;
		int normal = 0;
		int i = 0;
		while(it.hasNext()){
			i++;
			if(i == 1){
				warn = ((BigInteger)it.next()).intValue();
			}else if(i == 2){
				normal = ((BigInteger)it.next()).intValue();
			}
		}

		ModelAndView model = new ModelAndView("row/" + realField);
		model.addObject("overtubes", warn);
		model.addObject("tubes", normal);
		return model;
	}
	
	public ModelAndView getMacroscopicPieRow(String field, int rowId){
		
		List list = facade.getMacroscopicPieRow(field, rowId);
		Iterator it = list.iterator();
		int warn = 0;
		int normal = 0;
		int i = 0;
		while(it.hasNext()){
			i++;
			if(i == 1){
				warn = ((BigInteger)it.next()).intValue();
			}else if(i == 2){
				normal = ((BigInteger)it.next()).intValue();
			}
		}

		ModelAndView model = new ModelAndView("row/" + field);
		model.addObject("overtubes", warn);
		model.addObject("tubes", normal);
		return model;
	}

	public ModelAndView getRealityPieSurface(String realField, String warnField, int surfaceId){
		
		List list = facade.getRealityPieSurface(realField, warnField, surfaceId);
		Iterator it = list.iterator();
		int warn = 0;
		int normal = 0;
		int i = 0;
		while(it.hasNext()){
			i++;
			if(i == 1){
				warn = ((BigInteger)it.next()).intValue();
			}else if(i == 2){
				normal = ((BigInteger)it.next()).intValue();
			}
		}

		ModelAndView model = new ModelAndView("surface/" + realField);
		model.addObject("overtubes", warn);
		model.addObject("tubes", normal);
		return model;
	}
	
	public ModelAndView getMacroscopicPieSurface(String field, int surfaceId){
		
		List list = facade.getMacroscopicPieSurface(field, surfaceId);
		Iterator it = list.iterator();
		int warn = 0;
		int normal = 0;
		int i = 0;
		while(it.hasNext()){
			i++;
			if(i == 1){
				warn = ((BigInteger)it.next()).intValue();
			}else if(i == 2){
				normal = ((BigInteger)it.next()).intValue();
			}
		}

		ModelAndView model = new ModelAndView("surface/" + field);
		model.addObject("overtubes", warn);
		model.addObject("tubes", normal);
		return model;
	}
	
	public ModelAndView getRealityPieBoiler(String realField, String warnField, int boilerId){
		
		List list = facade.getRealityPieBoiler(realField, warnField, boilerId);
		Iterator it = list.iterator();
		int warn = 0;
		int normal = 0;
		int i = 0;
		while(it.hasNext()){
			i++;
			if(i == 1){
				warn = ((BigInteger)it.next()).intValue();
			}else if(i == 2){
				normal = ((BigInteger)it.next()).intValue();
			}
		}

		ModelAndView model = new ModelAndView("boiler/" + realField);
		model.addObject("overtubes", warn);
		model.addObject("tubes", normal);
		return model;
	}
	
	public ModelAndView getMacroscopicPieBoiler(String field, int boilerId){
		
		List list = facade.getMacroscopicPieBoiler(field, boilerId);
		Iterator it = list.iterator();
		int warn = 0;
		int normal = 0;
		int i = 0;
		while(it.hasNext()){
			i++;
			if(i == 1){
				warn = ((BigInteger)it.next()).intValue();
			}else if(i == 2){
				normal = ((BigInteger)it.next()).intValue();
			}
		}

		ModelAndView model = new ModelAndView("boiler/" + field);
		model.addObject("overtubes", warn);
		model.addObject("tubes", normal);
		return model;
	}
}
